package ar.com.ford.it.fullEconomics.view.action;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.facade.CommodityFacade;
import ar.com.ford.it.fullEconomics.facade.VehiclelinFacade;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp01Part;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;
import ar.com.ford.it.fullEconomics.view.form.PartsWithoutCostStructReportForm;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.facade.FacadeProvider;
import ar.com.ford.it.webengine.struts.AbstractBaseAction;
import ar.com.ford.it.webengine.struts.FordActionMapping;
import ar.com.ford.it.webengine.websession.WebSession;

public class PartsWithoutCostStructReportAction extends AbstractBaseAction {

    /**
     * Setea el mensaje de "busqueda sin resultados" cuando la busqueda no trae
     * resultados
     * 
     * @param request
     * Request donde se guarda el mensaje
     */
    private void searchWithNoResults(HttpServletRequest request) {
        ActionMessages am = new ActionMessages();
        MessageResources messageResources = (MessageResources) getResources(request);

        am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                "customMessage", messageResources
                        .getMessage("message.noResults")));
        saveMessages(request, am);
    }

    /**
     * Resetea el formulario y envia a la pagina inicial de Parts
     * 
     */
    public ActionForward initPage(FordActionMapping mapping,
                                  ActionForm actionForm,
                                  HttpServletRequest request,
                                  HttpServletResponse response,
                                  WebSession webSession, ActionMessages msgs,
                                  ActionMessages warns, ActionMessages errorMsgs) {

        ActionMessages am = new ActionMessages();
        PartsWithoutCostStructReportForm form = (PartsWithoutCostStructReportForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CommodityFacade facade = (CommodityFacade) FacadeProvider.getFacade(
                request, CommodityFacade.class);
        VehiclelinFacade vLineFacade = (VehiclelinFacade) FacadeProvider
                .getFacade(request, VehiclelinFacade.class);
        request.getSession().setAttribute("partClassColl",
                facade.getPartClases());
        try {
            request.getSession().setAttribute("commColl",
                    facade.findCommodities("", "", user.getTeesg01Plant()));
            request.getSession().setAttribute("lineColl",
                    vLineFacade.findAllVehiclelin());
        } catch (FullEconomicsException e) {
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }

        form.resetForm();
        return mapping.findForward("success");
    }

    /**
     * Se encarga del manejo de la busqueda de Parts
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward findParts(FordActionMapping mapping,
                                   ActionForm actionForm,
                                   HttpServletRequest request,
                                   HttpServletResponse response,
                                   WebSession webSession, ActionMessages msgs,
                                   ActionMessages warns,
                                   ActionMessages errorMsgs) {

        ActionMessages am = new ActionMessages();

        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        PartsWithoutCostStructReportForm form = (PartsWithoutCostStructReportForm) actionForm;
        CommodityFacade facade = (CommodityFacade) FacadeProvider.getFacade(
                request, CommodityFacade.class);
        Collection<Teesp01Part> res = null;
        try {
            Timestamp dateInMin = null;
            Timestamp dateInMax = null;
            Timestamp dateOutMin = null;
            Timestamp dateOutMax = null;
            if (!StringUtils.isBlank(form.getDateInMin())) {
                Calendar calDateInMin = FullEconomicsDateUtil.format(form
                        .getDateInMin());
                dateInMin = new Timestamp(calDateInMin.getTimeInMillis());
            }
            if (!StringUtils.isBlank(form.getDateOutMin())) {
                Calendar calDateOutMin = FullEconomicsDateUtil.format(form
                        .getDateOutMin());
                dateOutMin = new Timestamp(calDateOutMin.getTimeInMillis());
            }
            if (!StringUtils.isBlank(form.getDateInMax())) {
                Calendar calDateInMax = FullEconomicsDateUtil.format(form
                        .getDateInMax());
                dateInMax = new Timestamp(calDateInMax.getTimeInMillis());
            }
            if (!StringUtils.isBlank(form.getDateOutMax())) {
                Calendar calDateOutMax = FullEconomicsDateUtil.format(form
                        .getDateOutMax());
                dateOutMax = new Timestamp(calDateOutMax.getTimeInMillis());
            }
            Teesp01Part findPart = new Teesp01Part();
            findPart.setEesp01PartPrefixCodeC(form.getPrefix());
            findPart.setEesp01PartBaseCodeC(form.getBase());
            findPart.setEesp01PartSuffixCodeC(form.getSuffix());
            findPart.setEesp01PartClassCodeC(form.getPartClass());
            findPart.setEesp01PartInDateY(dateInMin);
            findPart.setEesp01PartOutDateY(dateOutMin);
            findPart.setEesp01PlantCodeC(user.getTeesg01Plant());
            res = facade.findPartsWithoutCostStr(findPart, dateInMax,
                    dateOutMax, form.getSupplierClass(),
                    form.getSupplierCode(), form.getCommodity(), form
                            .getVehicleLine());
            if (res.isEmpty()) {
                searchWithNoResults(request);
            }
        } catch (FullEconomicsException e) {
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }
        form.setParts(res);
        return mapping.findForward("success");
    }

}
